package org.bigdata.framework.admin.web.controller;

import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.bigdata.framework.admin.web.util.DateUtils;
import org.bigdata.framework.admin.web.util.JudgeDate;
import org.bigdata.framework.core.model.BalanceAccount;
import org.bigdata.framework.core.model.CountAccount;
import org.bigdata.framework.core.model.Log;
import org.bigdata.framework.core.model.Member;
import org.bigdata.framework.core.model.MemberProject;
import org.bigdata.framework.core.model.Project;
import org.bigdata.framework.core.model.ProjectProduct;
import org.bigdata.framework.core.model.RepositoryProduct;
import org.bigdata.framework.core.model.TimeAccount;
import org.bigdata.framework.core.model.User;
import org.bigdata.framework.core.protocol.IBaseService;
import org.bigdata.framework.core.protocol.IPayService;
import org.bigdata.framework.core.protocol.IPublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class PayController extends BaseContoroller {

	@Autowired
	private IBaseService baseService;
	@Autowired
	private IPublicService publicService;
	@Autowired
	private IPayService payService;

	//公共日常记录
	public void log(Integer createUserId,String logType,String classesId,String dataId,String logText,Integer logStatus) throws Exception{
		//获得本机IP 
		InetAddress addr = InetAddress.getLocalHost();   
		String createIpAddress=addr.getHostAddress();  
		//获得时间
		String createTime = DateUtils.getDateTime();
		
		Log log=new Log();
		log.setCreateTime(createTime);
		log.setCreateUserId(createUserId);
		log.setCreateIpAddress(createIpAddress);
		log.setLogType(logType);
		log.setLogText(logText);
		log.setLogStatus(logStatus);
			
		if(classesId !=null){
			log.setClassesId(classesId);
		}
		if(dataId !=null){
			log.setDataId(dataId);
		}
		baseService.saveLog(log);
	}
	
	// 交易管理 -->机构订购资源管理
	@RequestMapping("application-organization-order.html")
	public ModelAndView toPayOrganizationOrder() {
		ModelAndView view = new ModelAndView();
		view.addObject("project", payService.getProject());
		view.setViewName("/modules/pay/pay_organization_order");
		return view;
	}

	// 交易管理 -->机构订购资源管理--添加
	@RequestMapping("pay_organization_order_add_update.html")
	public String toPayOrganizationOrderAdd() {

		return "/modules/pay/pay_organization_order_add_update";
	}

	// 交易管理 -->项目管理
	@RequestMapping("application-project-management.html")
	public ModelAndView toProjectManagement() {
		ModelAndView view = new ModelAndView();
		view.addObject("repositoryProduct", publicService.getRepositoryProduct());
		view.setViewName("/modules/pay/pay_project_management");
		return view;
	}

	// 交易管理 -->项目管理--添加
	@RequestMapping("pay_project_management_add_update.html")
	public String toPayProjectManagementAdd() {

		return "/modules/pay/pay_project_management_add_update";
	}	
	// 交易管理 -->项目管理--点击查看详情
	@RequestMapping("pay_project_management_details.html")
	public String toPayProjectManagementDetail() {

		return "/modules/pay/pay_project_management_details";
	}
	/**
	 * 实现交易管理 -->项目管理
	 */
	//交易管理 -->项目管理-->默认查询页面
	@RequestMapping("productfindAll")
	@ResponseBody
	public void listAll(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Integer pageNumber = 15;
		Integer pageSize = Integer.parseInt(request.getParameter("page"));
		String nameEn = request.getParameter("nameEn");
		String nameZh = request.getParameter("nameZh");
		String type = request.getParameter("type");
		String accountType = request.getParameter("accountType");
		String repositoryProductId = request.getParameter("repositoryProductId");
		HashMap<String, Object> queryParams = new HashMap<String, Object>();
		if(nameEn != null && !"".equals(nameEn)){
			queryParams.put("%,name_en", nameEn);
		}
		if(nameZh != null && !"".equals(nameZh)){
			queryParams.put("%,name_zh", nameZh);
		}
		if(type != null && !"".equals(type)){
			queryParams.put("=,type", type);
		}
		if(accountType != null && !"".equals(accountType)){
			queryParams.put("=,account_type", accountType);
		}
		if(repositoryProductId != null && !"".equals(repositoryProductId)){
			queryParams.put("%,product_detail", repositoryProductId+",");
		}
		/*if (Integer.parseInt(type) == 0) {
			queryParams.put("=,type", type);
		} else if (Integer.parseInt(type) == 2) {
			queryParams.put("=,type", type);
		}*/
		queryParams.put("^,sort", "asc");
		Integer count = payService.getProjectCount(queryParams);
		List<HashMap<String, Object>> r = new ArrayList<HashMap<String, Object>>();
		LinkedHashMap<String, Object> mp = null;
		List<Project> list = payService.getProject(queryParams, pageSize,
				pageNumber);
		for (int i = 0; i < list.size(); i++) {
			Project project = list.get(i);
			List<LinkedHashMap<String, Object>> listmapHashMaps= new ArrayList<LinkedHashMap<String,Object>>();
			if(project.getProductDetail() != null && !"".equals(project.getProductDetail())){
			String [] projects =project.getProductDetail().split(",");
			String productDetailName="";
				for (int j = 0; j < projects.length; j++) {
					LinkedHashMap<String, Object> repositoryProductMap = new LinkedHashMap<String, Object>();
					RepositoryProduct rp=publicService.findRepositoryProduct(Integer.parseInt(projects[j]));
					 if(rp != null){
						 repositoryProductMap.put("nameZh", rp.getName());
						 repositoryProductMap.put("id", rp.getId());
						 listmapHashMaps.add(repositoryProductMap);
						 productDetailName+=rp.getName()+",";
					 }
				}
			}
			mp = new LinkedHashMap<String, Object>();
			mp.put("id", project.getId());
			mp.put("nameEn", project.getNameEn());
			mp.put("nameZh", project.getNameZh());
			mp.put("type", project.getType());
			mp.put("accountType", project.getAccountType());
			mp.put("productDetail", listmapHashMaps);
			mp.put("description", project.getDescription());
			mp.put("sort", project.getSort());
			if (project.getId() != null && !"".equals(project.getId())) {
				Project department = payService.findProject(project.getId());
				mp.put("departmentName", department.getNameEn());
			} else {
				mp.put("departmentName", "");
			}
			r.add(mp);
		}

		String result = formatToJson(r);
		StringBuffer sb = new StringBuffer();
		int listsize = r.size();
		sb.append("{\"listsize\":" + listsize + ",");
		sb.append("\"pageSize\":"
				+ (int) Math.ceil(Double.valueOf(count) / pageNumber) + ",");
		sb.append("\"result\":" + result);
		sb.append("}");
		super.pringWriterToPage(sb.toString(), "application/json", response);
	}
	//交易管理 -->项目管理-->添加页面
	@RequestMapping("productAddUpdate")
	@ResponseBody
	public void productAdd(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String id = request.getParameter("id");
		String nameEn = request.getParameter("nameEn");
		String nameZh = request.getParameter("nameZh");
		String type = request.getParameter("type");
		String accountType = request.getParameter("accountType");
		//String productDetail = request.getParameter("productDetail");
		String[] productDetail =request.getParameterValues("productDetail");
		String description = request.getParameter("description");
		//String sort = request.getParameter("sort");
		String productDetails ="";
		if(productDetail != null && productDetail.length>0){
			for (int i = 0; i < productDetail.length; i++) {
				productDetails += productDetail[i]+",";
			}
		}
		Project project = new Project();
		project.setNameEn(nameEn);
		project.setNameZh(nameZh);
		project.setType(type);
		project.setAccountType(accountType);
		project.setProductDetail(productDetails);
		project.setDescription(description);
		long sorts =payService.getProjectCount(null)+1;
		project.setSort((int)sorts);
		User user = publicService.findUser((Integer) request.getSession().getAttribute("userid"));
		int logTime=0;
		// 新增
		if ("0".equals(id)) {
			
			try {
				payService.saveProject(project);
			} catch (Exception e) {
				this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【项目表】 失败！请检查数据是否正确！", 1);
				logTime=1;
				e.printStackTrace();
			}
			if(logTime==0){
				this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【项目表】成功！", 0);
			}
			
		} else {
			// 修改
			
			try {
				project.setId(Integer.parseInt(id));
				payService.editProject(project);
			} catch (Exception e) {
				this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【修改】【项目表】 失败！请检查数据是否正确！", 1);
				logTime=1;
				e.printStackTrace();
			}
			if(logTime==0){
				this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【修改】【项目表】成功！", 0);
			}
			
		}

		String result = formatToJson(project);
		super.pringWriterToPage(result.toString(), "application/json", response);
	}
	// 交易管理 -->项目管理-->默认ids查询
		@RequestMapping("repositoryIds")
		@ResponseBody
		public void queryIds(HttpServletRequest request,
				HttpServletResponse response) throws Exception {
			HashMap<String, Object> queryParams = new LinkedHashMap<String, Object>();
			queryParams.put("=,status", 0);
			List<RepositoryProduct> repositoryProduct=publicService.getRepositoryProduct(queryParams, 1, publicService.getRepositoryProductCount(queryParams));
			String result = formatToJson(repositoryProduct);
			//System.out.println(result);
			super.pringWriterToPage(result.toString(), "application/json", response);
		}
	//交易管理 -->项目管理-->修改页面
	@RequestMapping("productUpdate")
	@ResponseBody
	public void toProductUpdate(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Integer id = Integer.parseInt(request.getParameter("id"));
		Project project = payService.findProject(id);
		String result = formatToJson(project);
		super.pringWriterToPage(result.toString(), "application/json", response);
	
	}
	// 交易管理 -->项目管理-->查看详情
		@RequestMapping("projectManagementDetail")
		public void toPayProjectManagementDetail(HttpServletRequest request,
				HttpServletResponse response) throws Exception {
			Integer id = Integer.parseInt(request.getParameter("id"));
			RepositoryProduct repositoryProduct = publicService.findRepositoryProduct(id);
			String result = formatToJson(repositoryProduct);
			super.pringWriterToPage(result.toString(), "application/json", response);
		}
	//交易管理 -->项目管理-->删除页面
	@RequestMapping("projectdel")
	public void toProjectDel(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Integer id = Integer.parseInt(request.getParameter("id"));

		int logTime=0;
		List<String> r = new ArrayList<String>();
		String rString = "yes";
		User user = publicService.findUser((Integer) request.getSession().getAttribute("userid"));
		
		try {
			Project project = payService.findProject(id);
			HashMap<String, Object> queryParams = new LinkedHashMap<String, Object>();
			queryParams.put("=,project_id", project.getId());
			int count = payService.getMemberProjectCount(queryParams);
			if(count == 0){
				payService.deleteProject(project);
			}else{
				rString = "no";
			}
			
		}catch (Exception e) {
			this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【项目表】 失败！请检查数据是否正确！", 1);
			logTime=1;
			e.printStackTrace();
		}
		if(logTime==0){
			this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【项目表】成功！", 0);
		}
		r.add(rString);
		String result = formatToJson(r);
		super.pringWriterToPage(result.toString(), "application/json", response);
	}
	/**
	 * 实现交易管理 -->机构订购资源管理
	 */
	/**
	 * 实现交易管理 -->机构订购资源管理  start
	 * 
	 * @author zf
	 * 20017.2.9
	 */
	
	//交易管理 -->机构订购资源管理-->默认查询页面
	@RequestMapping("queryOrganizationOrder")
	public void organizationOrder(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String page = request.getParameter("page");
		String username = request.getParameter("username");
		String realname = request.getParameter("realname");
		String type = request.getParameter("type");
		String projectId = request.getParameter("projectId");
		
		//查询机构用户
		HashMap<String, Object> queryParams = new HashMap<String, Object>();
		if(username != null && !"".equals(username)){
			queryParams.put("%,username", username);
		}
		if(realname != null && !"".equals(realname)){
			queryParams.put("%,realname", realname);
		}
		if(type != null && !"".equals(type)){
			queryParams.put("=,type", type);
		}
		/*if(projectId != null && !"".equals(projectId)){
			HashMap<String, Object> mProjectParams = new HashMap<String, Object>();
			mProjectParams.put("=,project_id", projectId);
			List<MemberProject> mProjectList=payService.getMemberProject(mProjectParams, 1, payService.getMemberProjectCount(mProjectParams).intValue());
			for (int i = 0; i < mProjectList.size(); i++) {
				MemberProject MemberProject = mProjectList.get(i);
				queryParams.put("=,type", MemberProject.g);
			}
		}*/
		//机构用户结果集
		List<Member> memberList=publicService.getMember(queryParams, Integer.parseInt(page), 15);
		
		//机构用户总数
		long memberCount =publicService.getMemberCount(queryParams);
		//页数
		int count = 0;
		//获得结果集
		List<HashMap<String, Object>> arraylist = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < memberList.size(); i++) {
			Member member= memberList.get(i);
			HashMap<String, Object> memberProjectParams = new HashMap<String, Object>();
			memberProjectParams.put("=,memberId", member.getId());
			int memberProjectCount =payService.getMemberProjectCount(null).intValue();
			
			List<MemberProject> memberProjectList=payService.getMemberProject(memberProjectParams, 1, memberProjectCount);
			for (int k = 0; k < memberProjectList.size(); k++) {
				MemberProject memberProject =memberProjectList.get(k);
				if(member.getId().equals(memberProject.getMemberId())){
					count ++;
					//结果
					HashMap<String, Object> consequence = new LinkedHashMap<String, Object>();
					consequence.put("realName", member.getRealname());
					consequence.put("userName", member.getUsername());
					consequence.put("id", member.getId());
					List<HashMap<String, Object>> orderResourcelist = new ArrayList<HashMap<String, Object>>();
					for (int j = 0; j < memberProjectList.size(); j++) {
						HashMap<String, Object> orderResource = new LinkedHashMap<String, Object>();
						MemberProject aMemberProject =memberProjectList.get(j);
						Project project=payService.findProject(aMemberProject.getProjectId());
						if(project!=null) {
							orderResource.put("text", project.getNameZh()+"("+project.getType()+")");
						}else {
							orderResource.put("text", "");
						}
						
						orderResource.put("id", project.getId());
						orderResource.put("nameZh", project.getNameZh());
						orderResource.put("type", project.getType());
						orderResource.put("detailId", aMemberProject.getProductDetailId());
						orderResourcelist.add(orderResource);
					}
					consequence.put("orderResource", orderResourcelist);
					arraylist.add(consequence);
					break;
				}
			}
		}
		
		//其他结果
		HashMap<String, Object> restsData = new LinkedHashMap<String, Object>();
		int ye =(int)Math.ceil(Double.valueOf(count)/15);
		restsData.put("curr", ye);
		arraylist.add(restsData);
		
		String resultString = formatToJson(arraylist);
		super.pringWriterToPage(resultString, "application/json", response);
	}
	
		// 交易管理 -->机构订购资源管理新增页
		@RequestMapping("pay-organization-order-add.html")
		public ModelAndView topayOrganizationOrderAdd() {
			List<Project> projectText = new ArrayList<Project>();
	        List<Project> projectList =payService.getProject();
	        for (int i = 0; i < projectList.size(); i++) {
	        	Project project=projectList.get(i);
				if(project.getAccountType().equals("Group")){
					projectText.add(project);
				}
			}
	        
			ModelAndView view = new ModelAndView();
			view.addObject("projectList",projectText);
			view.setViewName("/modules/pay/pay_organization_order_add");
			return view;
		}
		// 机构订购资源管理新增页 -->机构用户列表页
		@RequestMapping("pay-organization-user.html")
		public String toPayOrganizationUser() {
			return "/modules/pay/pay_organization_user";
		}
		
		// 机构订购资源管理新增知识库
		@RequestMapping("organizationUserProduct")
		public void organizationUserProduct(HttpServletRequest request,
				HttpServletResponse response) throws Exception {
			String projectId =request.getParameter("projectId");
			List<RepositoryProduct> repositoryProducts = new ArrayList<RepositoryProduct>();
			
			Project project=payService.findProject(Integer.parseInt(projectId));
			String [] productDetails =project.getProductDetail().split(",");
			for (int i = 0; i < productDetails.length; i++) {
				RepositoryProduct repositoryProduct = publicService.findRepositoryProduct(Integer.parseInt(productDetails[i]));
				if(repositoryProduct.getStatus() == 0){
					repositoryProducts.add(repositoryProduct);
				}
			}
			String result = formatToJson(repositoryProducts);
			//System.out.println(result);
			super.pringWriterToPage(result.toString(), "application/json", response);
		}
		
		//机构订购资源管理列表页-->新增
		@RequestMapping("organizationUserAdd")
		public void organizationUserAdd(HttpServletRequest request,HttpServletResponse response) throws Exception {
			String balanceMap =request.getParameter("balanceMap");
			String timeMap =request.getParameter("timeMap");
			String countMap =request.getParameter("countMap");
			User user = publicService.findUser((Integer) request.getSession().getAttribute("userid"));
			//转JSON
			JSONObject balance = JSONObject.fromObject(balanceMap); 
			JSONObject time = JSONObject.fromObject(timeMap); 
			JSONObject count = JSONObject.fromObject(countMap); 
			
			if(balanceMap !="{}" && !balanceMap.equals("{}")){
				BalanceAccount balanceAccount= new BalanceAccount();
				balanceAccount.setMemberId(Integer.parseInt(balance.get("organizationUserid").toString()));
				balanceAccount.setProjectId(Integer.parseInt(balance.get("projectId").toString()));
				balanceAccount.setStartTime(balance.get("start").toString());
				balanceAccount.setEndTime(balance.get("end").toString());
				balanceAccount.setBalance(balance.get("balance").toString());
				balanceAccount.setTotalConsume("0");
				int logTime=0;
				BalanceAccount abalanceAccount =null;
				try {
					abalanceAccount =  payService.saveBalanceAccount(balanceAccount);
				} catch (Exception e) {
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【余额账户】 失败！请检查数据是否正确！", 1);
					logTime=1;
					e.printStackTrace();
				}
				if(logTime==0){
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【余额账户】成功！", 0);
				}
				
				//会员-项目表新增数据
				MemberProject memberProject = new MemberProject();
				memberProject.setMemberId(Integer.parseInt(balance.get("organizationUserid").toString()));
				memberProject.setProjectId(Integer.parseInt(balance.get("projectId").toString()));
				memberProject.setProductDetailId(abalanceAccount.getId());
				
				try {
					payService.saveMemberProject(memberProject);
				} catch (Exception e) {
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【会员-项目表】 失败！请检查数据是否正确！", 1);
					logTime=1;
					e.printStackTrace();
				}
				if(logTime==0){
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【会员-项目表】成功！", 0);
				}
				
				//项目-产品库表新增数据
				if(!balance.get("project").toString().equals("") && balance.get("project").toString() !=null){
				String [] stringArr= balance.get("project").toString().split(",");
				for (int i = 0; i < stringArr.length; i++) {
					ProjectProduct projectProduct = new ProjectProduct();
					projectProduct.setRepositoryProductId(Integer.parseInt(stringArr[i]));
					projectProduct.setProjectId(Integer.parseInt(balance.get("projectId").toString()));
					projectProduct.setMemberId(Integer.parseInt(balance.get("organizationUserid").toString()));
					projectProduct.setProductDetailId(abalanceAccount.getId());
					try {
						payService.saveProjectProduct(projectProduct);
					} catch (Exception e) {
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【项目-产品库表】 失败！请检查数据是否正确！", 1);
						logTime=1;
						e.printStackTrace();
					}
					if(logTime==0){
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【项目-产品库表】成功！", 0);
					}
				}
				}
			}
			if(timeMap !="{}" && !timeMap.equals("{}")){
				TimeAccount timeAccount = new TimeAccount();
				timeAccount.setMemberId(time.get("organizationUserid").toString());
				timeAccount.setProjectId(time.get("projectId").toString());
				timeAccount.setStartTime(time.get("start").toString());
				timeAccount.setEndTime(time.get("end").toString());
				int logTime=0;
				TimeAccount aTimeAccount = null;
				try {
					aTimeAccount = payService.saveTimeAccount(timeAccount);
				} catch (Exception e) {
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【限时账户表】 失败！请检查数据是否正确！", 1);
					logTime=1;
					e.printStackTrace();
				}
				if(logTime==0){
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【限时账户表】成功！", 0);
				}
				
				//会员-项目表新增数据
				MemberProject memberProject = new MemberProject();
				memberProject.setMemberId(Integer.parseInt(time.get("organizationUserid").toString()));
				memberProject.setProjectId(Integer.parseInt(time.get("projectId").toString()));
				memberProject.setProductDetailId(aTimeAccount.getId());
				try {
					payService.saveMemberProject(memberProject);
				} catch (Exception e) {
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【会员-项目表】 失败！请检查数据是否正确！", 1);
					logTime=1;
					e.printStackTrace();
				}
				if(logTime==0){
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【会员-项目表】成功！", 0);
				}
				
				
				//项目-产品库表新增数据
				if(!time.get("project").toString().equals("") && time.get("project").toString() !=null){
				String [] stringArr= time.get("project").toString().split(",");
				for (int i = 0; i < stringArr.length; i++) {
					ProjectProduct projectProduct = new ProjectProduct();
					projectProduct.setRepositoryProductId(Integer.parseInt(stringArr[i]));
					projectProduct.setProjectId(Integer.parseInt(time.get("projectId").toString()));
					projectProduct.setMemberId(Integer.parseInt(time.get("organizationUserid").toString()));
					projectProduct.setProductDetailId(aTimeAccount.getId());
					try {
						payService.saveProjectProduct(projectProduct);
					} catch (Exception e) {
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【项目-产品库表】 失败！请检查数据是否正确！", 1);
						logTime=1;
						e.printStackTrace();
					}
					if(logTime==0){
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【项目-产品库表】成功！", 0);
					}
					
				}
				}
			}
			if(countMap !="{}" && !countMap.equals("{}")){
				CountAccount countAccount = new CountAccount();
				countAccount.setMemberId(Integer.parseInt(count.get("organizationUserid").toString()));
				countAccount.setProjectId(Integer.parseInt(count.get("projectId").toString()));
				countAccount.setStartTime(count.get("start").toString());
				countAccount.setEndTime(count.get("end").toString());
				countAccount.setCount(Integer.parseInt(count.get("surplus").toString()));
				countAccount.setCountConsume(Integer.parseInt(count.get("accumulative").toString()));
				int logTime=0;
				CountAccount aCountAccount= null;
				try {
					aCountAccount = payService.saveCountAccount(countAccount);
				} catch (Exception e) {
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【次数账户表】 失败！请检查数据是否正确！", 1);
					logTime=1;
					e.printStackTrace();
				}
				if(logTime==0){
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【次数账户表】成功！", 0);
				}


				
				//会员-项目表新增数据
				MemberProject memberProject = new MemberProject();
				memberProject.setMemberId(Integer.parseInt(count.get("organizationUserid").toString()));
				memberProject.setProjectId(Integer.parseInt(count.get("projectId").toString()));
				memberProject.setProductDetailId(aCountAccount.getId());
				try {
					payService.saveMemberProject(memberProject);
				} catch (Exception e) {
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【会员-项目表】 失败！请检查数据是否正确！", 1);
					logTime=1;
					e.printStackTrace();
				}
				if(logTime==0){
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【会员-项目表】成功！", 0);
				}

				
				//项目-产品库表新增数据
				if(!count.get("project").toString().equals("") && count.get("project").toString() !=null){
				String [] stringArr= count.get("project").toString().split(",");
				for (int i = 0; i < stringArr.length; i++) {
					ProjectProduct projectProduct = new ProjectProduct();
					projectProduct.setRepositoryProductId(Integer.parseInt(stringArr[i]));
					projectProduct.setProjectId(Integer.parseInt(count.get("projectId").toString()));
					projectProduct.setMemberId(Integer.parseInt(count.get("organizationUserid").toString()));
					projectProduct.setProductDetailId(aCountAccount.getId());
					try {
						payService.saveProjectProduct(projectProduct);
					} catch (Exception e) {
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【项目-产品库表】 失败！请检查数据是否正确！", 1);
						logTime=1;
						e.printStackTrace();
					}
					if(logTime==0){
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【项目-产品库表】成功！", 0);
					}

					
				}
				}
			}
		}
		
		// 机构订购资源管理页 --删除功能
		@RequestMapping("organizationUserDelete")
		public void organizationUserDelete(HttpServletRequest request,HttpServletResponse response) throws Exception {
			String id = request.getParameter("id");
			HashMap<String, Object> memberProjectParams = new HashMap<String, Object>();
			memberProjectParams.put("=,memberId", id);
			long cunt = payService.getMemberProjectCount(memberProjectParams);
			User user = publicService.findUser((Integer) request.getSession().getAttribute("userid"));
			List<MemberProject> memberProjectsList =payService.getMemberProject(memberProjectParams, 1, (int)cunt);
			for (int j = 0; j < memberProjectsList.size(); j++) {
			MemberProject memberProjects =memberProjectsList.get(j);
			//删除余额账户表
			HashMap<String, Object> balanceParams = new LinkedHashMap<String, Object>();
			balanceParams.put("=,memberId", memberProjects.getMemberId());
			balanceParams.put("=,projectId", memberProjects.getProjectId());
			long balancecount= payService.getBalanceAccountCount(null);
			List<BalanceAccount> balanceAccountList= payService.getBalanceAccount(balanceParams, 1, (int)balancecount);
			int logTime=0;
			for (int i = 0; i < balanceAccountList.size(); i++) {
				BalanceAccount balanceAccount =balanceAccountList.get(i);
				BalanceAccount balance = new BalanceAccount();
				
				try {
					balance.setId(balanceAccount.getId());
					payService.deleteBalanceAccount(balance);
				} catch (Exception e) {
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【余额账户表】 失败！请检查数据是否正确！", 1);
					logTime=1;
					e.printStackTrace();
				}
				if(logTime==0){
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【余额账户表】成功！", 0);
				}
				
			}
			//删除次数账户表
			HashMap<String, Object> countParams = new LinkedHashMap<String, Object>();
			countParams.put("=,memberId", memberProjects.getMemberId());
			countParams.put("=,projectId", memberProjects.getProjectId());
			long countcecount= payService.getCountAccountCount(null);
			List<CountAccount> countAccountList= payService.getCountAccount(countParams, 1, (int)countcecount);
			for (int i = 0; i < countAccountList.size(); i++) {
				CountAccount countAccount =countAccountList.get(i);
				CountAccount count = new CountAccount();
				
				try {
					count.setId(countAccount.getId());
					payService.deleteCountAccount(count);
				} catch (Exception e) {
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【次数账户表】 失败！请检查数据是否正确！", 1);
					logTime=1;
					e.printStackTrace();
				}
				if(logTime==0){
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【次数账户表】成功！", 0);
				}

				
			}
			//删除项目-产品库表
			HashMap<String, Object> projectProductParams = new LinkedHashMap<String, Object>();
			projectProductParams.put("=,projectId", memberProjects.getProjectId());
			projectProductParams.put("=,memberId", memberProjects.getMemberId());
			projectProductParams.put("=,productDetailId", memberProjects.getProductDetailId());
			long projectProduccount= payService.getProjectProductCount(null);
			List<ProjectProduct> projectProductList= payService.getProjectProduct(projectProductParams, 1, (int)projectProduccount);
			for (int i = 0; i < projectProductList.size(); i++) {
				ProjectProduct projectProduct =projectProductList.get(i);
				ProjectProduct project = new ProjectProduct();
				
				try {
					project.setId(projectProduct.getId());
					payService.deleteProjectProduct(project);
				} catch (Exception e) {
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【项目-产品库表】 失败！请检查数据是否正确！", 1);
					logTime=1;
					e.printStackTrace();
				}
				if(logTime==0){
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【项目-产品库表】成功！", 0);
				}

				
			}
			//删除限时账户表
			HashMap<String, Object> timeAccountParams = new LinkedHashMap<String, Object>();
			timeAccountParams.put("=,projectId", memberProjects.getProjectId());
			timeAccountParams.put("=,memberId", memberProjects.getMemberId());
			long timeAccountcount= payService.getTimeAccountCount(null);
			List<TimeAccount> timeAccountList= payService.getTimeAccount(timeAccountParams, 1, (int)timeAccountcount);
			for (int i = 0; i < timeAccountList.size(); i++) {
				TimeAccount timeAccount =timeAccountList.get(i);
				TimeAccount time = new TimeAccount();
				
				try {
					time.setId(timeAccount.getId());
					payService.deleteTimeAccount(time);
				} catch (Exception e) {
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【限时账户表】 失败！请检查数据是否正确！", 1);
					logTime=1;
					e.printStackTrace();
				}
				if(logTime==0){
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【限时账户表】成功！", 0);
				}

				
			}
			//删除会员-项目表
			try {
				payService.deleteMemberProject(memberProjects);
			} catch (Exception e) {
				this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【会员-项目表】 失败！请检查数据是否正确！", 1);
				logTime=1;
				e.printStackTrace();
			}
			if(logTime==0){
				this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【会员-项目表】成功！", 0);
			}

			}
		}
		// 机构订购资源管理 -->单个修改页面
		@RequestMapping("pay-organization-order-upload.html")
		public String toPayOrganizationUpload() {
			return "/modules/pay/pay_organization_order_upload";
		}
		// 交易管理 -->机构订购资源管理新增+功能
		@RequestMapping("pay-organization-order-addsumbit.html")
		public ModelAndView toPayOrganizationAddSumbit(HttpServletRequest request,HttpServletResponse response) {
			String id = request.getParameter("id");
			List<Project> textList =new ArrayList<Project>();
			
	        List<Project> projectList =payService.getProject();
	        
	        HashMap<String, Object> queryParams = new HashMap<String, Object>();
	        queryParams.put("=,memberId", id);
	        int membersize= payService.getMemberProjectCount(queryParams).intValue();
	        List<MemberProject> memberProjectList=payService.getMemberProject(queryParams, 1, membersize);
	        	for (int j = 0; j < projectList.size(); j++) {
	        		Project project=projectList.get(j);
	        		int test=0;
	        		for (int i = 0; i < memberProjectList.size(); i++) {
	    	        	MemberProject memberProject=memberProjectList.get(i);
						if(project.getId().equals(memberProject.getProjectId())){
							test=1;
							break;
					}
				}
	        	if(test == 0 && project.getAccountType().equals("Group")){
						textList.add(project);
				}
			}
	        
	        ModelAndView view = new ModelAndView();
			view.addObject("projectList",textList);
			view.setViewName("/modules/pay/pay_organization_order_addsumbit");
			return view;
		}
		
		// 机构订购资源管理 -->单个修改页面默认内容
		@RequestMapping("organizationUserUploadQuery")
		public void organizationUserUploadQuery(HttpServletRequest request,HttpServletResponse response) throws Exception {
			String type = request.getParameter("type");
			String projectId = request.getParameter("projectId");
			String memberId = request.getParameter("memberId");
			String detailId = request.getParameter("detailId");
			
			HashMap<String, Object> text = new LinkedHashMap<String, Object>();
			if(type.equals("balance")){
			    BalanceAccount balanceAccount =payService.findBalanceAccount(Integer.parseInt(detailId));
				text.put("id", balanceAccount.getId());
				text.put("memberId", balanceAccount.getMemberId());
				text.put("projectId", balanceAccount.getProjectId());
				text.put("startTim", balanceAccount.getStartTime());
				text.put("endTime", balanceAccount.getEndTime());
				text.put("totalConsume", balanceAccount.getTotalConsume());
				text.put("balance", balanceAccount.getBalance());
			}
			if(type.equals("time")){
				TimeAccount TimeAccount =payService.findTimeAccount(Integer.parseInt(detailId));
					text.put("id", TimeAccount.getId());
					text.put("memberId", TimeAccount.getMemberId());
					text.put("projectId", TimeAccount.getProjectId());
					text.put("startTim", TimeAccount.getStartTime());
					text.put("endTime", TimeAccount.getEndTime());
			}
			if(type.equals("count")){
				CountAccount countAccount  =payService.findCountAccount(Integer.parseInt(detailId));
					text.put("id", countAccount.getId());
					text.put("memberId", countAccount.getMemberId());
					text.put("projectId", countAccount.getProjectId());
					text.put("startTim", countAccount.getStartTime());
					text.put("endTime", countAccount.getEndTime());
					text.put("count", countAccount.getCount());
					text.put("countConsume", countAccount.getCountConsume());
			}
			HashMap<String, Object> projectProductParams = new LinkedHashMap<String, Object>();
			projectProductParams.put("=,projectId", projectId);
			projectProductParams.put("=,memberId", memberId);
			projectProductParams.put("=,productDetailId", detailId);
			int projectProductSize =payService.getProjectProductCount(projectProductParams);
			List<ProjectProduct> ProjectProductList = payService.getProjectProduct(projectProductParams, 1, projectProductSize);
			String projectProducts="";
			for (int i = 0; i < ProjectProductList.size(); i++) {
				ProjectProduct projectProduct =ProjectProductList.get(i);
				projectProducts += projectProduct.getRepositoryProductId()+";";
			}
			text.put("projectProducts", projectProducts);
			String result = formatToJson(text);
			super.pringWriterToPage(result.toString(), "application/json", response);
		}
		
		//机构订购资源管理列表页-->单个修改
		@RequestMapping("organizationUserUpalod")
		public void organizationUserUpalod(HttpServletRequest request,HttpServletResponse response) throws Exception {
			String balanceMap =request.getParameter("balanceMap");
			String timeMap =request.getParameter("timeMap");
			String countMap =request.getParameter("countMap");
			String id = request.getParameter("id");
			int logTime=0;
			User user = publicService.findUser((Integer) request.getSession().getAttribute("userid"));
			//转JSON
			JSONObject balance = JSONObject.fromObject(balanceMap); 
			JSONObject time = JSONObject.fromObject(timeMap); 
			JSONObject count = JSONObject.fromObject(countMap); 
			
			if(balanceMap !="{}" && !balanceMap.equals("{}")){
				BalanceAccount balanceAccount= new BalanceAccount();
				balanceAccount.setId(Integer.parseInt(id));
				balanceAccount.setMemberId(Integer.parseInt(balance.get("organizationUserid").toString()));
				balanceAccount.setProjectId(Integer.parseInt(balance.get("projectId").toString()));
				balanceAccount.setStartTime(balance.get("start").toString());
				balanceAccount.setEndTime(balance.get("end").toString());
				balanceAccount.setBalance(balance.get("balance").toString());
				balanceAccount.setTotalConsume("0");
				BalanceAccount aBalanceAccount=null;
				try {
					aBalanceAccount = payService.editBalanceAccount(balanceAccount);
				} catch (Exception e) {
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【修改】【余额账户表】 失败！请检查数据是否正确！", 1);
					logTime=1;
					e.printStackTrace();
				}
				if(logTime==0){
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【修改】【余额账户表】成功！", 0);
				}

				
				
				
				//项目-产品库表新增数据
				String [] stringArr= balance.get("project").toString().split(",");
				
				HashMap<String, Object> projectProductParams = new LinkedHashMap<String, Object>();
				projectProductParams.put("=,memberId", balance.get("organizationUserid").toString());
				projectProductParams.put("=,projectId",balance.get("projectId").toString());
				projectProductParams.put("=,productDetailId",aBalanceAccount.getId());
				
				int projectProductCountlong = payService.getProjectProductCount(projectProductParams).intValue();
				List<ProjectProduct> projectProductList=payService.getProjectProduct(projectProductParams,1, projectProductCountlong);
				for (int i = 0; i < projectProductList.size(); i++) {
					
					try {
						ProjectProduct projectProduct=projectProductList.get(i);
						payService.deleteProjectProduct(projectProduct);
					} catch (Exception e) {
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【项目-产品库表】 失败！请检查数据是否正确！", 1);
						logTime=1;
						e.printStackTrace();
					}
					if(logTime==0){
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【项目-产品库表】成功！", 0);
					}
					
				}
				
				for (int i = 0; i < stringArr.length; i++) {
					ProjectProduct projectProduct = new ProjectProduct();
					if(stringArr[i] != null && !"".equals(stringArr[i])){
					    projectProduct.setRepositoryProductId(Integer.parseInt(stringArr[i]));
					}
					projectProduct.setProjectId(Integer.parseInt(balance.get("projectId").toString()));
					projectProduct.setMemberId(Integer.parseInt(balance.get("organizationUserid").toString()));
					projectProduct.setProductDetailId(aBalanceAccount.getId());
					try {
						payService.saveProjectProduct(projectProduct);
					} catch (Exception e) {
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【项目-产品库表】 失败！请检查数据是否正确！", 1);
						logTime=1;
						e.printStackTrace();
					}
					if(logTime==0){
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【项目-产品库表】成功！", 0);
					}


					
				}
			}
			if(timeMap !="{}" && !timeMap.equals("{}")){
				TimeAccount timeAccount = new TimeAccount();
				timeAccount.setId(Integer.parseInt(id));
				timeAccount.setMemberId(time.get("organizationUserid").toString());
				timeAccount.setProjectId(time.get("projectId").toString());
				timeAccount.setStartTime(time.get("start").toString());
				timeAccount.setEndTime(time.get("end").toString());
				TimeAccount aTimeAccount=null;
				try {
					aTimeAccount = payService.editTimeAccount(timeAccount);
				} catch (Exception e) {
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【限时账户表】 失败！请检查数据是否正确！", 1);
					logTime=1;
					e.printStackTrace();
				}
				if(logTime==0){
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【限时账户表】成功！", 0);
				}
				
				
				//项目-产品库表新增数据
				HashMap<String, Object> projectProductParams = new LinkedHashMap<String, Object>();
				projectProductParams.put("=,memberId", time.get("organizationUserid").toString());
				projectProductParams.put("=,projectId",time.get("projectId").toString());
				projectProductParams.put("=,productDetailId",aTimeAccount.getId());
				long projectProductCountlong = payService.getProjectProductCount(projectProductParams);
				List<ProjectProduct> projectProductList=payService.getProjectProduct(projectProductParams, 1, (int)projectProductCountlong);
				for (int i = 0; i < projectProductList.size(); i++) {
					
					try {
						ProjectProduct projectProduct=projectProductList.get(i);
						payService.deleteProjectProduct(projectProduct);
					} catch (Exception e) {
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【项目-产品库表】 失败！请检查数据是否正确！", 1);
						logTime=1;
						e.printStackTrace();
					}
					if(logTime==0){
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【项目-产品库表】成功！", 0);
					}

					
				}
				
				String [] stringArr= time.get("project").toString().split(",");
				for (int i = 0; i < stringArr.length; i++) {
					ProjectProduct projectProduct = new ProjectProduct();
					if(stringArr[i] != null && !"".equals(stringArr[i])){
                        projectProduct.setRepositoryProductId(Integer.parseInt(stringArr[i]));
                    }
					projectProduct.setProjectId(Integer.parseInt(time.get("projectId").toString()));
					projectProduct.setMemberId(Integer.parseInt(time.get("organizationUserid").toString()));
					projectProduct.setProductDetailId(aTimeAccount.getId());
					try {
						payService.saveProjectProduct(projectProduct);
					} catch (Exception e) {
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【知识库产品表】 失败！请检查数据是否正确！", 1);
						logTime=1;
						e.printStackTrace();
					}
					if(logTime==0){
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【知识库产品表】成功！", 0);
					}
					
				}
			}
			
			if(countMap !="{}" && !countMap.equals("{}")){
				CountAccount countAccount = new CountAccount();
				countAccount.setId(Integer.parseInt(id));
				countAccount.setMemberId(Integer.parseInt(count.get("organizationUserid").toString()));
				countAccount.setProjectId(Integer.parseInt(count.get("projectId").toString()));
				countAccount.setStartTime(count.get("start").toString());
				countAccount.setEndTime(count.get("end").toString());
				countAccount.setCount(Integer.parseInt(count.get("surplus").toString()));
				countAccount.setCountConsume(Integer.parseInt(count.get("accumulative").toString()));
				CountAccount aCountAccount=null;
				try {
					aCountAccount = payService.editCountAccount(countAccount);
				} catch (Exception e) {
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【修改】【次数账户表】 失败！请检查数据是否正确！", 1);
					logTime=1;
					e.printStackTrace();
				}
				if(logTime==0){
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【修改】【次数账户表】成功！", 0);
				}
				
				//项目-产品库表新增数据
				HashMap<String, Object> projectProductParams = new LinkedHashMap<String, Object>();
				projectProductParams.put("=,memberId", count.get("organizationUserid").toString());
				projectProductParams.put("=,projectId",count.get("projectId").toString());
				projectProductParams.put("=,productDetailId",aCountAccount.getId());
				long projectProductCountlong = payService.getProjectProductCount(projectProductParams);
				List<ProjectProduct> projectProductList=payService.getProjectProduct(projectProductParams, 1, (int)projectProductCountlong);
				for (int i = 0; i < projectProductList.size(); i++) {
					
					try {
						ProjectProduct projectProduct=projectProductList.get(i);
						payService.deleteProjectProduct(projectProduct);
					} catch (Exception e) {
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【项目-产品库表】 失败！请检查数据是否正确！", 1);
						logTime=1;
						e.printStackTrace();
					}
					if(logTime==0){
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【项目-产品库表】成功！", 0);
					}
					
				}
				String [] stringArr= count.get("project").toString().split(",");
				for (int i = 0; i < stringArr.length; i++) {
					ProjectProduct projectProduct = new ProjectProduct();
					if(stringArr[i] != null && !"".equals(stringArr[i])){
                        projectProduct.setRepositoryProductId(Integer.parseInt(stringArr[i]));
                    }
					projectProduct.setProjectId(Integer.parseInt(count.get("projectId").toString()));
					projectProduct.setMemberId(Integer.parseInt(count.get("organizationUserid").toString()));
					projectProduct.setProductDetailId(aCountAccount.getId());
					try {
						payService.saveProjectProduct(projectProduct);
					} catch (Exception e) {
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【知识库产品表】 失败！请检查数据是否正确！", 1);
						logTime=1;
						e.printStackTrace();
					}
					if(logTime==0){
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【知识库产品表】成功！", 0);
					}

					
				}
			}
		}
		//机构订购资源管理列表页-->单个修改-断开关系
		@RequestMapping("organizationUserdeleterelation")
		public void organizationUserdeleterelation(HttpServletRequest request,HttpServletResponse response) throws Exception {
			String projectId =request.getParameter("projectId");
			String type =request.getParameter("type");
			String memberId =request.getParameter("memberId");
			String detailId =request.getParameter("detailId");
			int logTime=0;
			User user = publicService.findUser((Integer) request.getSession().getAttribute("userid"));
			HashMap<String, Object> queryParams = new LinkedHashMap<String, Object>();
			queryParams.put("=,projectId", projectId);
			queryParams.put("=,memberId", memberId);
			queryParams.put("=,productDetailId", detailId);
			if(type.equals("balance")){
				BalanceAccount balanceAccount = payService.findBalanceAccount(Integer.parseInt(detailId));
					try {
						payService.deleteBalanceAccount(balanceAccount);
					} catch (Exception e) {
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【余额账户表】 失败！请检查数据是否正确！", 1);
						logTime=1;
						e.printStackTrace();
					}
					if(logTime==0){
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【余额账户表】成功！", 0);
					}
			}
			if(type.equals("time")){
				TimeAccount timeAccount = payService.findTimeAccount(Integer.parseInt(detailId));
					try {
						payService.deleteTimeAccount(timeAccount);
					} catch (Exception e) {
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【限时账户表】 失败！请检查数据是否正确！", 1);
						logTime=1;
						e.printStackTrace();
					}
					if(logTime==0){
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【限时账户表】成功！", 0);
					}
			}
			if(type.equals("count")){
				CountAccount countAccount = payService.findCountAccount(Integer.parseInt(detailId));
					try {
						payService.deleteCountAccount(countAccount);
					} catch (Exception e) {
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【次数账户表】 失败！请检查数据是否正确！", 1);
						logTime=1;
						e.printStackTrace();
					}
					if(logTime==0){
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【次数账户表】成功！", 0);
					}
			}
			
			int projectProductSize =payService.getProjectProductCount(queryParams).intValue();
			List<ProjectProduct> projectProductList=payService.getProjectProduct(queryParams, 1, projectProductSize);
			for (int i = 0; i < projectProductList.size(); i++) {
				try {
					ProjectProduct projectProduct =projectProductList.get(i);
					payService.deleteProjectProduct(projectProduct);
				} catch (Exception e) {
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【项目-产品库表】 失败！请检查数据是否正确！", 1);
					logTime=1;
					e.printStackTrace();
				}
				if(logTime==0){
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【项目-产品库表】成功！", 0);
				}
				
			}
			
			int memberProjectSize =payService.getMemberProjectCount(queryParams).intValue();
			List<MemberProject> memberProjectList=payService.getMemberProject(queryParams, 1, memberProjectSize);
			for (int i = 0; i < memberProjectList.size(); i++) {
				
				try {
					MemberProject memberProject =memberProjectList.get(i);
					payService.deleteMemberProject(memberProject);
				} catch (Exception e) {
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【会员-项目表】 失败！请检查数据是否正确！", 1);
					logTime=1;
					e.printStackTrace();
				}
				if(logTime==0){
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【会员-项目表】成功！", 0);
				}
				
			}
		}
		// 机构订购资源管理-->编辑页面
		@RequestMapping("pay-organization-order-uploadAll.html")
		public ModelAndView toPayOrganizationUploadALL(HttpServletRequest request,HttpServletResponse response) {
			String id = request.getParameter("id");
			List<Project> textList =new ArrayList<Project>();
			
			List<Project> projectList =payService.getProject();
			
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
			queryParams.put("=,memberId", id);
			int membersize= payService.getMemberProjectCount(queryParams).intValue();
			List<MemberProject> memberProjectList=payService.getMemberProject(queryParams, 1, membersize);
			for (int j = 0; j < projectList.size(); j++) {
				Project project=projectList.get(j);
				int test=0;
				for (int i = 0; i < memberProjectList.size(); i++) {
					MemberProject memberProject=memberProjectList.get(i);
					if(project.getId().equals(memberProject.getProjectId())){
						test=1;
						break;
					}
				}
				if(test == 0 && project.getAccountType().equals("Group")) {
					textList.add(project);
				}
			}
			
			ModelAndView view = new ModelAndView();
			view.addObject("projectList",textList);
			view.setViewName("/modules/pay/pay_organization_order_uploadAll");
			return view;
		}
		//修改全部下拉筛选查询
		@RequestMapping("organizationUserUploadquery")
		public void organizationUserUploadquery(HttpServletRequest request,HttpServletResponse response) throws Exception{
			String memberId=request.getParameter("memberId");
			List<Project> projectList = new ArrayList<Project>();
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
			queryParams.put("=,memberId", memberId);
			int membersize= payService.getMemberProjectCount(queryParams).intValue();
			List<MemberProject> memberProjectList=payService.getMemberProject(queryParams, 1, membersize);
			for (int i = 0; i < memberProjectList.size(); i++) {
				projectList.add(payService.findProject(memberProjectList.get(i).getProjectId()));
			}
			String result = formatToJson(projectList);
			super.pringWriterToPage(result.toString(), "application/json", response);
		}
		// 机构订购资源管理 -->单个修改页面默认内容
		@RequestMapping("organizationUserUploadupladeText")
		public void organizationUserUploadupladeText(HttpServletRequest request,HttpServletResponse response) throws Exception {
			String memberId = request.getParameter("memberId");
			
			List<HashMap<String, Object>> listMap =new ArrayList<HashMap<String,Object>>();
			
			String projectIds="";
			
			HashMap<String, Object> queryMemberParams = new HashMap<String, Object>();
			queryMemberParams.put("=,memberId", memberId);
			int membersize= payService.getMemberProjectCount(queryMemberParams).intValue();
			List<MemberProject> memberProjectList=payService.getMemberProject(queryMemberParams, 1, membersize);
			for (int j = 0; j < memberProjectList.size(); j++) {
				Project project = payService.findProject(memberProjectList.get(j).getProjectId());
				projectIds +=project.getId()+";"+project.getType()+",";
				if(project.getType().equals("balance")){
					HashMap<String, Object> text = new LinkedHashMap<String, Object>();
					HashMap<String, Object> queryParams = new HashMap<String, Object>();
					queryParams.put("=,memberId", memberId);
					queryParams.put("=,projectId", project.getId());
					long pageSize = payService.getBalanceAccountCount(queryParams);
					List<BalanceAccount> balanceAccountList =payService.getBalanceAccount(queryParams, 1, (int)pageSize);
					for (int i = 0; i < balanceAccountList.size(); i++) {
						BalanceAccount balanceAccount =balanceAccountList.get(i);
						text.put("id", balanceAccount.getId());
						text.put("memberId", balanceAccount.getMemberId());
						text.put("projectId", balanceAccount.getProjectId());
						text.put("startTim", balanceAccount.getStartTime());
						text.put("endTime", balanceAccount.getEndTime());
						text.put("totalConsume", balanceAccount.getTotalConsume());
						text.put("balance", balanceAccount.getBalance());
						text.put("type", "balance");
					}
					long projectProductSize =payService.getProjectProductCount(queryParams);
					List<ProjectProduct> ProjectProductList = payService.getProjectProduct(queryParams, 1, (int)projectProductSize);
					String projectProducts="";
					for (int i = 0; i < ProjectProductList.size(); i++) {
						ProjectProduct projectProduct =ProjectProductList.get(i);
						projectProducts += projectProduct.getRepositoryProductId()+";";
					}
					text.put("projectProducts", projectProducts);
					listMap.add(text);
				}
				if(project.getType().equals("time")){
					HashMap<String, Object> text = new LinkedHashMap<String, Object>();
					HashMap<String, Object> queryParams = new HashMap<String, Object>();
					queryParams.put("=,memberId", memberId);
					queryParams.put("=,projectId", project.getId());
					long pageSize = payService.getTimeAccountCount(queryParams);
					List<TimeAccount> timeAccountList =payService.getTimeAccount(queryParams, 1, (int)pageSize);
					for (int i = 0; i < timeAccountList.size(); i++) {
						TimeAccount TimeAccount =timeAccountList.get(i);
						text.put("id", TimeAccount.getId());
						text.put("memberId", TimeAccount.getMemberId());
						text.put("projectId", TimeAccount.getProjectId());
						text.put("startTim", TimeAccount.getStartTime());
						text.put("endTime", TimeAccount.getEndTime());
						text.put("type", "time");
					}
					long projectProductSize =payService.getProjectProductCount(queryParams);
					List<ProjectProduct> ProjectProductList = payService.getProjectProduct(queryParams, 1, (int)projectProductSize);
					String projectProducts="";
					for (int i = 0; i < ProjectProductList.size(); i++) {
						ProjectProduct projectProduct =ProjectProductList.get(i);
						projectProducts += projectProduct.getRepositoryProductId()+";";
					}
					text.put("projectProducts", projectProducts);
					listMap.add(text);
				}
				if(project.getType().equals("count")){
					HashMap<String, Object> text = new LinkedHashMap<String, Object>();
					HashMap<String, Object> queryParams = new HashMap<String, Object>();
					queryParams.put("=,memberId", memberId);
					queryParams.put("=,projectId", project.getId());
					long pageSize = payService.getCountAccountCount(queryParams);
					List<CountAccount> countAccountList =payService.getCountAccount(queryParams, 1, (int)pageSize);
					for (int i = 0; i < countAccountList.size(); i++) {
						CountAccount countAccount =countAccountList.get(i);
						text.put("id", countAccount.getId());
						text.put("memberId", countAccount.getMemberId());
						text.put("projectId", countAccount.getProjectId());
						text.put("startTim", countAccount.getStartTime());
						text.put("endTime", countAccount.getEndTime());
						text.put("count", countAccount.getCount());
						text.put("countConsume", countAccount.getCountConsume());
						text.put("type", "count");
					}
					long projectProductSize =payService.getProjectProductCount(queryParams);
					List<ProjectProduct> ProjectProductList = payService.getProjectProduct(queryParams, 1, (int)projectProductSize);
					String projectProducts="";
					for (int i = 0; i < ProjectProductList.size(); i++) {
						ProjectProduct projectProduct =ProjectProductList.get(i);
						projectProducts += projectProduct.getRepositoryProductId()+";";
					}
					text.put("projectProducts", projectProducts);
					listMap.add(text);
				}
			}
			HashMap<String, Object> projectIdsMap = new HashMap<String, Object>();
			projectIdsMap.put("projectIds", projectIds.substring(0,projectIds.length()-1));
			listMap.add(projectIdsMap);
			String result = formatToJson(listMap);
			super.pringWriterToPage(result.toString(), "application/json", response);
		}
		//机构订购资源管理-->全部编辑
		@RequestMapping("organizationUserUpalodEdit")
		public void organizationUserUpalodEdit(HttpServletRequest request,HttpServletResponse response) throws Exception {
			String balanceMap =request.getParameter("balanceMap");
			String timeMap =request.getParameter("timeMap");
			String countMap =request.getParameter("countMap");
			int logTime=0;
			User user = publicService.findUser((Integer) request.getSession().getAttribute("userid"));
			//转JSON
			JSONObject balance = JSONObject.fromObject(balanceMap); 
			JSONObject time = JSONObject.fromObject(timeMap); 
			JSONObject count = JSONObject.fromObject(countMap); 
			
			if(balanceMap !="{}" && !balanceMap.equals("{}")){
				BalanceAccount balanceAccount= new BalanceAccount();
				balanceAccount.setMemberId(Integer.parseInt(balance.get("organizationUserid").toString()));
				balanceAccount.setProjectId(Integer.parseInt(balance.get("projectId").toString()));
				balanceAccount.setStartTime(balance.get("start").toString());
				balanceAccount.setEndTime(balance.get("end").toString());
				balanceAccount.setBalance(balance.get("balance").toString());
				balanceAccount.setTotalConsume("0");
				BalanceAccount aBalanceAccount = null;
				try {
					if(!balance.get("id").toString().equals("0")){
						balanceAccount.setId(Integer.parseInt(balance.get("id").toString()));
						aBalanceAccount = payService.editBalanceAccount(balanceAccount);
					}else{
						aBalanceAccount = payService.saveBalanceAccount(balanceAccount);
						//会员-项目表新增数据
						MemberProject memberProject = new MemberProject();
						memberProject.setMemberId(Integer.parseInt(balance.get("organizationUserid").toString()));
						memberProject.setProjectId(Integer.parseInt(balance.get("projectId").toString()));
						memberProject.setProductDetailId(aBalanceAccount.getId());
						payService.saveMemberProject(memberProject);
					}
				} catch (Exception e) {
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【修改】【余额账户表】 失败！请检查数据是否正确！", 1);
					logTime=1;
					e.printStackTrace();
				}
				if(logTime==0){
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【修改】【余额账户表】成功！", 0);
				}
				
				

				//项目-产品库表新增数据
				String [] stringArr= balance.get("project").toString().split(",");
				
				HashMap<String, Object> projectProductParams = new LinkedHashMap<String, Object>();
				projectProductParams.put("=,memberId", balance.get("organizationUserid").toString());
				projectProductParams.put("=,projectId",balance.get("projectId").toString());
				projectProductParams.put("=,productDetailId", aBalanceAccount.getId());
				int projectProductCountlong = payService.getProjectProductCount(projectProductParams).intValue();
				List<ProjectProduct> projectProductList=payService.getProjectProduct(projectProductParams,1, projectProductCountlong);
				for (int i = 0; i < projectProductList.size(); i++) {
					
					try {
						ProjectProduct projectProduct=projectProductList.get(i);
						payService.deleteProjectProduct(projectProduct);
					} catch (Exception e) {
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【项目-产品库表】 失败！请检查数据是否正确！", 1);
						logTime=1;
						e.printStackTrace();
					}
					if(logTime==0){
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【项目-产品库表】成功！", 0);
					}

					
				}
				
				for (int i = 0; i < stringArr.length; i++) {
					ProjectProduct projectProduct = new ProjectProduct();
					projectProduct.setRepositoryProductId(Integer.parseInt(stringArr[i]));
					projectProduct.setProjectId(Integer.parseInt(balance.get("projectId").toString()));
					projectProduct.setMemberId(Integer.parseInt(balance.get("organizationUserid").toString()));
					projectProduct.setProductDetailId(aBalanceAccount.getId());
					
					try {
						payService.saveProjectProduct(projectProduct);
					} catch (Exception e) {
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【项目-产品库表】 失败！请检查数据是否正确！", 1);
						logTime=1;
						e.printStackTrace();
					}
					if(logTime==0){
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【项目-产品库表】成功！", 0);
					}

				}
			}
			if(timeMap !="{}" && !timeMap.equals("{}")){
				TimeAccount timeAccount = new TimeAccount();
				timeAccount.setMemberId(time.get("organizationUserid").toString());
				timeAccount.setProjectId(time.get("projectId").toString());
				timeAccount.setStartTime(time.get("start").toString());
				timeAccount.setEndTime(time.get("end").toString());
				TimeAccount aTimeAccount =null;
				try {
					if(!time.get("id").toString().equals("0")){
						timeAccount.setId(Integer.parseInt(time.get("id").toString()));
						aTimeAccount = payService.editTimeAccount(timeAccount);
					}else{
						aTimeAccount = payService.saveTimeAccount(timeAccount);
						//会员-项目表新增数据
						MemberProject memberProject = new MemberProject();
						memberProject.setMemberId(Integer.parseInt(time.get("organizationUserid").toString()));
						memberProject.setProjectId(Integer.parseInt(time.get("projectId").toString()));
						memberProject.setProductDetailId(aTimeAccount.getId());
						payService.saveMemberProject(memberProject);
					}
				} catch (Exception e) {
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【修改】【限时账户表】 失败！请检查数据是否正确！", 1);
					logTime=1;
					e.printStackTrace();
				}
				if(logTime==0){
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【修改】【限时账户表】成功！", 0);
				}
				
				//项目-产品库表新增数据
				HashMap<String, Object> projectProductParams = new LinkedHashMap<String, Object>();
				projectProductParams.put("=,memberId", time.get("organizationUserid").toString());
				projectProductParams.put("=,projectId",time.get("projectId").toString());
				projectProductParams.put("=,productDetailId",aTimeAccount.getId());
				long projectProductCountlong = payService.getProjectProductCount(projectProductParams);
				List<ProjectProduct> projectProductList=payService.getProjectProduct(projectProductParams, 1, (int)projectProductCountlong);
				for (int i = 0; i < projectProductList.size(); i++) {
					
					try {
						ProjectProduct projectProduct=projectProductList.get(i);
						payService.deleteProjectProduct(projectProduct);
					} catch (Exception e) {
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【项目-产品库表】 失败！请检查数据是否正确！", 1);
						logTime=1;
						e.printStackTrace();
					}
					if(logTime==0){
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【项目-产品库表】成功！", 0);
					}

					
				}
				
				String [] stringArr= time.get("project").toString().split(",");
				for (int i = 0; i < stringArr.length; i++) {
					ProjectProduct projectProduct = new ProjectProduct();
					projectProduct.setRepositoryProductId(Integer.parseInt(stringArr[i]));
					projectProduct.setProjectId(Integer.parseInt(time.get("projectId").toString()));
					projectProduct.setMemberId(Integer.parseInt(time.get("organizationUserid").toString()));
					projectProduct.setProductDetailId(aTimeAccount.getId());
					try {
						payService.saveProjectProduct(projectProduct);
					} catch (Exception e) {
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【项目-产品库表】 失败！请检查数据是否正确！", 1);
						logTime=1;
						e.printStackTrace();
					}
					if(logTime==0){
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【项目-产品库表】成功！", 0);
					}

					
				}
			}
			
			if(countMap !="{}" && !countMap.equals("{}")){
				CountAccount countAccount = new CountAccount();
				countAccount.setMemberId(Integer.parseInt(count.get("organizationUserid").toString()));
				countAccount.setProjectId(Integer.parseInt(count.get("projectId").toString()));
				countAccount.setStartTime(count.get("start").toString());
				countAccount.setEndTime(count.get("end").toString());
				countAccount.setCount(Integer.parseInt(count.get("surplus").toString()));
				countAccount.setCountConsume(Integer.parseInt(count.get("accumulative").toString()));
				CountAccount aCountAccount = null;
				try {
					if(!count.get("id").toString().equals("0")){
						countAccount.setId(Integer.parseInt(count.get("id").toString()));
						aCountAccount = payService.editCountAccount(countAccount);
					}else{
						aCountAccount = payService.saveCountAccount(countAccount);
						//会员-项目表新增数据
						MemberProject memberProject = new MemberProject();
						memberProject.setMemberId(Integer.parseInt(count.get("organizationUserid").toString()));
						memberProject.setProjectId(Integer.parseInt(count.get("projectId").toString()));
						memberProject.setProductDetailId(aCountAccount.getId());
						payService.saveMemberProject(memberProject);
					}
				} catch (Exception e) {
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【修改】【次数账户表】 失败！请检查数据是否正确！", 1);
					logTime=1;
					e.printStackTrace();
				}
				if(logTime==0){
					this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【修改】【次数账户表】成功！", 0);
				}
				
				//项目-产品库表新增数据
				HashMap<String, Object> projectProductParams = new LinkedHashMap<String, Object>();
				projectProductParams.put("=,memberId", count.get("organizationUserid").toString());
				projectProductParams.put("=,projectId",count.get("projectId").toString());
				projectProductParams.put("=,productDetailId",aCountAccount.getId());
				long projectProductCountlong = payService.getProjectProductCount(projectProductParams);
				List<ProjectProduct> projectProductList=payService.getProjectProduct(projectProductParams, 1, (int)projectProductCountlong);
				for (int i = 0; i < projectProductList.size(); i++) {
					
					try {
						ProjectProduct projectProduct=projectProductList.get(i);
						payService.deleteProjectProduct(projectProduct);
					} catch (Exception e) {
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【项目-产品库表】 失败！请检查数据是否正确！", 1);
						logTime=1;
						e.printStackTrace();
					}
					if(logTime==0){
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【删除】【项目-产品库表】成功！", 0);
					}

					
				}
				String [] stringArr= count.get("project").toString().split(",");
				for (int i = 0; i < stringArr.length; i++) {
					ProjectProduct projectProduct = new ProjectProduct();
					projectProduct.setRepositoryProductId(Integer.parseInt(stringArr[i]));
					projectProduct.setProjectId(Integer.parseInt(count.get("projectId").toString()));
					projectProduct.setMemberId(Integer.parseInt(count.get("organizationUserid").toString()));
					projectProduct.setProductDetailId(aCountAccount.getId());
					try {
						payService.saveProjectProduct(projectProduct);
					} catch (Exception e) {
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【项目-产品库表】 失败！请检查数据是否正确！", 1);
						logTime=1;
						e.printStackTrace();
					}
					if(logTime==0){
						this.log(user.getId(), "general_data", null, null, "【"+user.getNickname()+"】【新增】【项目-产品库表】成功！", 0);
					}
					
				}
			}
		}
		
	//判断时间范围
    @RequestMapping("goDateJudge")
    public void goDateJudge(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String startTimes=request.getParameter("startTimes");
        String mendId=request.getParameter("mendId");
        String dateProjectId=request.getParameter("dateProjectId");
        String judge = "yes";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String date = df.format(new Date())+" 00:00:00";
        LinkedHashMap<String, Object> r = new LinkedHashMap<String, Object>();
        if("yes".equals(JudgeDate.timeJudge(date,startTimes))){
            HashMap<String, Object> queryParams = new LinkedHashMap<String, Object>();
            queryParams.put("=,member_id", mendId);
            queryParams.put("=,project_id", dateProjectId);
            List<BalanceAccount> balanceAccountList = payService.getBalanceAccount(queryParams, 1, payService.getBalanceAccountCount(queryParams));
            if(!"no".equals(judge)){
                for (int i = 0; i < balanceAccountList.size(); i++) {
                    BalanceAccount balanceAccount = balanceAccountList.get(i);
                    judge = JudgeDate.dateJudge(balanceAccount.getStartTime(), balanceAccount.getEndTime(), startTimes);
                    if("no".equals(judge)){
                        break;
                    }
                }
            }
            if(!"no".equals(judge)){
                List<CountAccount> countAccountList = payService.getCountAccount(queryParams, 1, payService.getCountAccountCount(queryParams));
                for (int i = 0; i < countAccountList.size(); i++) {
                    CountAccount countAccount = countAccountList.get(i);
                    judge = JudgeDate.dateJudge(countAccount.getStartTime(), countAccount.getEndTime(), startTimes);
                    if("no".equals(judge)){
                        break;
                    }
                }
            }
            if(!"no".equals(judge)){
                List<TimeAccount> timeAccountList = payService.getTimeAccount(queryParams, 1, payService.getTimeAccountCount(queryParams));
                for (int i = 0; i < timeAccountList.size(); i++) {
                    TimeAccount timeAccount = timeAccountList.get(i);
                    judge = JudgeDate.dateJudge(timeAccount.getStartTime(), timeAccount.getEndTime(), startTimes);
                    if("no".equals(judge)){
                        break;
                    }
                }
            }
        }else{
            judge = "noNewTime"; 
        }
        r.put("judge", judge);
        String result = formatToJson(r);
        super.pringWriterToPage(result.toString(), "application/json", response);
    }	
    //判断时间大小
    @RequestMapping("goTimeJudge")
    public void goTimeJudge(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String startTimes=request.getParameter("startTimes");
        String endTimes=request.getParameter("endTimes");
        LinkedHashMap<String, Object> r = new LinkedHashMap<String, Object>();
        String judge = JudgeDate.timeJudge(startTimes, endTimes);
        r.put("judge", judge);
        String result = formatToJson(r);
        super.pringWriterToPage(result.toString(), "application/json", response);
    }	
	/** 实现交易管理 -->机构订购资源管理  end */

	
}
